package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.example.config.RocketMQConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/transaction")
public class TransactionController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private RocketMQConfigProperties prop;

    @RequestMapping("/send")
    public String send() {
        // 事务发送
        Message<String> message = MessageBuilder.withPayload("我是事务消息")
                .setHeader(RocketMQHeaders.KEYS, 1)
                .setHeader("orderID", 10)
                .setHeader(RocketMQHeaders.TRANSACTION_ID, 100).build();
        rocketMQTemplate.sendMessageInTransaction(prop.getTopic().get("transaction"), message, null);
        return "ok";
    }
}
